package cn.hzking.gjmall.delegates.category;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import cn.hzking.gjmall.R;
import cn.hzking.gjmall.activitys.second.CateGorySecondGoodListActivity;
import cn.hzking.gjmall.adpters.category.CategoryListAdapter;
import cn.hzking.gjmall.base.BaseDelegate;
import cn.hzking.gjmall.bean.CategoryBean;
import cn.hzking.gjmall.utils.Logger;

/**
 * A simple {@link Fragment} subclass.
 */
public class CategoryContentDelegate extends BaseDelegate {


    private static final String ARG_CONTENT_ID = "CINTENTID";
    private static final String CONTETSTRING = "contetString";
    @Bind(R.id.category_rv)
    RecyclerView categoryRv;
    @Bind(R.id.statlayout_c)
    StateLayout statlayoutC;
    private int mContentId; //一级商品id
    private String contetString;//需要显示的列表数据
    private ArrayList<String> itemContents = new ArrayList<>();//列表内容
    private int firstId = -1;//一级列表id
    private ArrayList<String> secondIds = new ArrayList<>();//二级条目列表

    public CategoryContentDelegate() {
        // Required empty public constructor
    }

    public static CategoryContentDelegate newInstance(int contentId, String contentString) {
        final Bundle args = new Bundle();
        args.putInt(ARG_CONTENT_ID, contentId);
        args.putString(CONTETSTRING, contentString);
        final CategoryContentDelegate delegate = new CategoryContentDelegate();
        delegate.setArguments(args);
        return delegate;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Bundle args = getArguments();
        if (args != null) {
            mContentId = args.getInt(ARG_CONTENT_ID);
            contetString = args.getString(CONTETSTRING);
        }
    }

    @Override
    public Object setLayout() {
        return R.layout.fragment_category_content_delegate;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        CategoryListAdapter categoryListAdapter = new CategoryListAdapter(R.layout.item_category_content_list);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(_mActivity, 3);
        categoryRv.setLayoutManager(gridLayoutManager);
        categoryRv.setAdapter(categoryListAdapter);
        Gson gson = new Gson();
        final CategoryBean.DataBean.CateListBean bean = gson.fromJson(contetString, CategoryBean.DataBean.CateListBean.class);
        final List<CategoryBean.DataBean.CateListBean.SubCategoriesBean> subCategories = bean.getSubCategories();
        //条目点击事件
        categoryListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                itemContents.clear();
                secondIds.clear();
                for (int i = 0; i < subCategories.size(); i++) {
                    itemContents.add(subCategories.get(i).getName());
                    secondIds.add(String.valueOf(subCategories.get(i).getId()));
                }
                itemContents.add(0, "全部");
                secondIds.add(0, String.valueOf(mContentId));
                Intent intent = new Intent(_mActivity, CateGorySecondGoodListActivity.class);
                intent.putExtra("index", position + 1);//条目在list中的位置
                intent.putStringArrayListExtra("secondId", secondIds);//二级条目id
                intent.putStringArrayListExtra("itemcontent", itemContents);//内容布局
                _mActivity.startActivity(intent);
                Logger.e("子");
            }


        });
        categoryListAdapter.setNewData(subCategories);
        if(categoryListAdapter.getData().size() == 0){
            statlayoutC.showCustomView(View.inflate(_mActivity,R.layout.layout_statelayout_custom_goodlist_empty,null));
        }
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
    }

}
